package com.fingard.dsp.bank.directbank.cdrcb01;

import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ConnState;
import com.fingard.net.WebRequest;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CDRCB01Base extends DirectBase {
	protected String getCharset() {
		return getBankConfig().getCharset("UTF-8");
	}
	
	protected String[] sendToBank(String p_reqStr, CDRCB01Header header, String type) throws Exception {
		// 提交签名
		Date tmpNowDate = new Date();
		String tmpAddrSign = bankFront.signURL;
		WriteBankLogLn("2.提交签名的地址：" + tmpAddrSign);
		WebRequest tmpWebRequestSign = new WebRequest(tmpAddrSign);
		tmpWebRequestSign.setConnectTimeout(60000);
		tmpWebRequestSign.setRequestProperty("Content-Type", "INFOSEC_SIGN/1.0");
		tmpWebRequestSign.setRequestProperty("connection", "Close");
		String signXml = sendToBankXml(p_reqStr);
		WriteBankLogLn("提交签名的报文：" + signXml);
		byte[] tmpToSignBytes = signXml.getBytes(getBankConfig().getCharset("GBK", "sign"));
		String[] tmpSignStr = tmpWebRequestSign.upload(tmpToSignBytes, getBankConfig().getCharset("GBK", "sign"));
		// String[]
		// tmpSignStr={"","<html><head><title>签名结果</title><result>0</result></head><body><sign>a</sign></body></html>"};
		WriteBankLogLn("签名返回的结果：" + tmpSignStr[1]);
		if (tmpSignStr[0].equals(ConnState.FAIL)) {
			return tmpSignStr;
		}
		XmlTextReader xmlReader = new XmlTextReader(tmpSignStr[1]);
		String signed_data = "";

		// 读取签名
		if (xmlReader.read("/html/head")) {
			String error_no = xmlReader.readedXElement.getSingleText("result");
			if (!error_no.equals("0")) {
				tmpSignStr[0] = ConnState.FAIL;
				return tmpSignStr;
			}
		}
		if (xmlReader.read("/html/body")) {
			signed_data = xmlReader.readedXElement.getSingleText("sign");
		} else {
			tmpSignStr[0] = ConnState.FAIL;
			return tmpSignStr;
		}

		// 发送全部报文
		String[] retStr = new String[] { "", "" };
		String tmpAddr = bankFront.serverURL;
		String tmpToSendInfo = header.tranCode + "|" + bankFront.org + "|" + Format.DateTime17Format.format(tmpNowDate)
				+ "|" + type + "#" + p_reqStr + signed_data;
		WriteBankLogLn("提交银行的地址：" + tmpAddr);
		WriteBankLogStep2("签名链接后发送的报文： " + tmpToSendInfo);
		byte[] tmpToWriteBytes = tmpToSendInfo.getBytes(getCharset());
		WebRequest tmpWebRequest = new WebRequest(tmpAddr);
		tmpWebRequest.setConnectTimeout(60000);
		tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		tmpWebRequest.setRequestProperty("connection", "Close");
		retStr = tmpWebRequest.upload(tmpToWriteBytes, getCharset());
		WriteBankLogStep3("接受的数据：" + retStr[1]);
		return retStr;
	}

	// 产生签名
	protected String sendToBankXml(String str) {
		XmlTextWriter xmlWriter = new XmlTextWriter();
		xmlWriter.writeStartDocByDeclaration(
				"<?xml version=1.0 encoding=\"" + getBankConfig().getCharset("GBK", "sign") + "\"?>");
		xmlWriter.writeStartElement("msg");
		xmlWriter.writeStartElement("msg_head");
		xmlWriter.writeElementString("msg_type", "0");
		xmlWriter.writeElementString("msg_id", "1005");
		xmlWriter.writeElementString("msg_sn", "0");
		xmlWriter.writeElementString("version", "1");
		xmlWriter.writeEndElement();
		xmlWriter.writeStartElement("msg_head");
		xmlWriter.writeElementString("origin_data_len", String.valueOf(str.length()));
		xmlWriter.writeElementString("origin_data", str);
		xmlWriter.writeEndDocument();
		return xmlWriter.toXmlString();
	}

	// 报文分割
	protected String[] splitRetToString(String strRet) throws Exception {
		String ret[] = new String[4];
		// 先按'|'分割
		String[] splitStr = strRet.split("\\|");
		String[] reSplitStr = splitStr[2].split("#");
		for (int i = 0; i < 3; i++) {
			ret[i] = splitStr[i];
		}
		ret[2] = reSplitStr[0];
		if (reSplitStr.length >= 2) {
			ret[3] = reSplitStr[1];
		}
		return ret;
	}
}
